<template>
    <DialogPayChooseM ref="dialogPayChooseRef" />
</template>

<script setup>
import { TicketOrderPay } from "~/api";

const dialogPayChooseRef = ref(null);

definePageMeta({
    layout: "none",
});

useSeoMeta({
    title: "门票支付-北京华锐云空间",
});

onMounted(async () => {
    const route = useRoute();
    const pageStore = usePageStore();
    const order_no = route.query["order_no"];
    const isWeixin = IsWeiXin();
    const isAlipay = IsAlipay();

    if (!isWeixin && !isAlipay) {
        dialogPayChooseRef.value.open(order_no);
    }

    if (isAlipay || (isWeixin && pageStore.code)) {
        const res = await TicketOrderPay({
            order_no,
            code: pageStore.code,
            pay_channel: isWeixin ? 1 : 2,
            redirect_url: "https://www.ykjhr.com",
        });

        if (res.code == 200) {
            if (isAlipay) {
                alipayForm(res.data);
            } else {
                WeixinJSBridge.invoke(
                    "getBrandWCPayRequest",
                    res.data,
                    function (res) {
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            Message().success("支付成功");
                            navigateTo("/");
                        } else if (
                            res.err_msg == "get_brand_wcpay_request:cancel"
                        ) {
                            Message().info("您取消了支付");
                        } else if (
                            res.err_msg == "get_brand_wcpay_request:fail"
                        ) {
                            Message().error("您取消了支付");
                        } else {
                            Message().warning(
                                typeof res.err_msg != "undefined"
                                    ? res.err_msg
                                    : typeof res.errMsg !== "undefined"
                                        ? res.errMsg
                                        : "未知支付状态"
                            );
                        }
                    }
                );
            }
        } else {
            alert(res.msg);
        }
    }
});
</script>
